package net.zjitc.doubao_community_backed.controller;/**
 * @Author 董熠涛
 * @Date 2025/6/19  23:57
 */

/**
 * @Author:董熠涛
 * @ 2025/6/1923:57
 */

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import net.zjitc.doubao_community_backed.api.ApiResult;
import net.zjitc.doubao_community_backed.model.dto.CreateTopicDTO;
import net.zjitc.doubao_community_backed.model.entity.BmsPost;
import net.zjitc.doubao_community_backed.model.entity.UmsUser;
import net.zjitc.doubao_community_backed.model.vo.PostVO;
import net.zjitc.doubao_community_backed.service.IBmsPostService;
import net.zjitc.doubao_community_backed.service.IUmsUserService;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static net.zjitc.doubao_community_backed.utils.JwtUtil.USER_NAME;

@RestController
@RequestMapping("/post")
public class BmsPostController {

    @Resource
    private IBmsPostService iBmsPostService;

    @Resource
    private IUmsUserService umsUserService;

    @GetMapping("/list")
    public ApiResult<Page<PostVO>> list(@RequestParam(value = "tab", defaultValue = "latest") String tab,
                                        @RequestParam(value = "pageNo", defaultValue = "1")  Integer pageNo,
                                        @RequestParam(value = "size", defaultValue = "10") Integer pageSize) {
        Page<PostVO> list = iBmsPostService.getList(new Page<>(pageNo, pageSize), tab);
        return ApiResult.success(list);
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ApiResult<BmsPost> create(@RequestHeader(value = USER_NAME) String userName
            , @RequestBody CreateTopicDTO dto) {
        UmsUser user = umsUserService.getUserByUsername(userName);
        BmsPost topic = iBmsPostService.create(dto, user);
        return ApiResult.success(topic);
    }

    @GetMapping()
    public ApiResult<Map<String, Object>> view(@RequestParam("id") String id) {
        Map<String, Object> map = iBmsPostService.viewTopic(id);
        return ApiResult.success(map);
    }
}